package GUI;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.awt.Font;

@SuppressWarnings("serial")
/**
 * Borrow a specific book copy for a specific user
 * @author Benny
 *
 */
public class BookBorrowGUI extends JPanel {
	private JTextField textCopyID;
	public MainFrame mf;
	private JTextField textUserID;
	/**
	 * initialize all the text fields of the GUI
	 */
	public void init(){
		textCopyID.setText("");
		textUserID.setText("");
	}
	
	/**
	 * check for the validity of the specified copyID
	 * @return copyID validity status
	 */
	public int checkCopyID(){
		try  
		{  
			@SuppressWarnings("unused")
			int i = Integer.parseInt(textCopyID.getText());  
		}  
		catch(NumberFormatException nfe)  
		{  
			return 0;  
		}  
		return 1;  
	}
	
	/**
	 * check for userID validity
	 * @return user's ID validity status
	 */
	public int checkID(){
        String expression = "^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{3})$";
        CharSequence inputStr = textUserID.getText();
        Pattern pattern = Pattern.compile(expression);
        Matcher matcher = pattern.matcher(inputStr);
        boolean matchFound = matcher.matches();

		if(matchFound) return 1;
		else return 0;
    }
	/**
	 * Create the panel.
	 * @param M Instance of the main Frame of the program
	 */
	public BookBorrowGUI(MainFrame M) {
		setLayout(null);
		mf=M;
		JLabel lblBookBorrow = new JLabel("Book Borrow");
		lblBookBorrow.setFont(new Font("Segoe UI", Font.BOLD, 16));
		lblBookBorrow.setBounds(262, 47, 160, 14);
		add(lblBookBorrow);
		
		JLabel lblCopyID = new JLabel("CopyID:");
		lblCopyID.setBounds(174, 91, 106, 14);
		add(lblCopyID);
		
		textCopyID = new JTextField();
		textCopyID.setBounds(290, 88, 160, 20);
		add(textCopyID);
		textCopyID.setColumns(10);
		
		JButton btnBorrow = new JButton("Borrow");
		btnBorrow.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if((checkCopyID()==1) && (checkID()==1))
				{
					ArrayList<Object> send = new ArrayList<>();
					int copyID=Integer.decode(textCopyID.getText());
					int userID=Integer.decode(textUserID.getText());
					send.add("2");
					send.add("BorrowBook");
					send.add(copyID);
					send.add(userID);
					mf.client.accept(send);
				}
				else JOptionPane.showMessageDialog(null, "Incorrect UserID / copyID", "Program", JOptionPane.ERROR_MESSAGE);
			}
		});
		btnBorrow.setBounds(174, 149, 89, 23);
		add(btnBorrow);
		
		JButton btnClear = new JButton("Clear");
		btnClear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				init();
			}
		});
		btnClear.setBounds(361, 149, 89, 23);
		add(btnClear);
		
		JLabel lblUserid = new JLabel("UserID");
		lblUserid.setBounds(174, 116, 46, 14);
		add(lblUserid);
		
		textUserID = new JTextField();
		textUserID.setBounds(290, 118, 160, 20);
		add(textUserID);
		textUserID.setColumns(10);
		
		ImageIcon img = new ImageIcon(getClass().getResource(
                "/util/grad2.jpg"));
		JLabel picLabel = new JLabel(img);
		picLabel.setLocation(0, 0);
		picLabel.setSize(683, 575);
		add(picLabel);

	}
}
